#include "ili9341_spi.h"

#define BUS_CLK_FREQ                    32000000
#define SPI_SLAVE_NUM                   1
#define SPI_FREQUENCY_MHZ               32
#define SPI_CLK_POLARITY                1
#define SPI_CLK_PHASE                   1
#define SPI_FRAME_FORMAT                0
#define SPI_FRAME_FORMAT_STANDARD       0
#define SPI_TMOD                        0
#define SPI_WAIT_CYCLES                 0x10



static spi_xfer_data_t GLOBAL_SPI_WRITE_DATA={0};
static uint8_t global_tx_data[1];
static uint8_t global_rx_data[1];

static void app_spi_init_pin(void)
{
    uapi_pin_set_mode(9, 3);
    uapi_pin_set_mode(11, 3);
    uapi_pin_set_mode(7, 3);
  //  uapi_pin_set_mode(CONFIG_SPI_CS_MASTER_PIN, CONFIG_SPI_MASTER_PIN_MODE);
    uapi_pin_set_mode(8, PIN_MODE_0);
    uapi_gpio_set_dir(8, GPIO_DIRECTION_OUTPUT);
    uapi_gpio_set_val(8, GPIO_LEVEL_HIGH);
}



static void app_spi_master_init_config(void)
{
    spi_attr_t config = { 0 };
    spi_extra_attr_t ext_config = { 0 };

    config.is_slave = false;
    config.slave_num = SPI_SLAVE_NUM;
    config.bus_clk = BUS_CLK_FREQ;
    config.freq_mhz = SPI_FREQUENCY_MHZ;
    config.clk_polarity = SPI_CLK_POLARITY;
    config.clk_phase = SPI_CLK_PHASE;
    config.frame_format = SPI_FRAME_FORMAT;
    config.spi_frame_format = HAL_SPI_FRAME_FORMAT_STANDARD;
    config.frame_size = HAL_SPI_FRAME_SIZE_8;// HAL_SPI_FRAME_SIZE_8;
    config.tmod = HAL_SPI_TRANS_MODE_TX;
    config.sste = 1;
    ext_config.qspi_param.wait_cycles = SPI_WAIT_CYCLES;
    ext_config.tx_use_dma = true;
    uapi_spi_init(0, &config, &ext_config);

#if defined(CONFIG_SPI_SUPPORT_DMA) && (CONFIG_SPI_SUPPORT_DMA == 1)
    uapi_dma_init();
    uapi_dma_open();

    spi_dma_config_t dma_cfg = {
        .src_width = 0,
        .dest_width = 0,
        .burst_length = 3,
        .priority = 0
    };
    if (uapi_spi_set_dma_mode(CONFIG_SPI_MASTER_BUS_ID, true, &dma_cfg) != ERRCODE_SUCC) {
        osal_printk("spi%d master set dma mode fail!\r\n");
    }

#endif  /* CONFIG_SPI_SUPPORT_DMA */

}

void spi_master_init(void){
    osal_printk("SPI init\r\n");
    /* SPI pinmux. */
    app_spi_init_pin();

    /* SPI master init config. */
    app_spi_master_init_config();

    osal_printk("SPI data init\r\n");
	GLOBAL_SPI_WRITE_DATA.tx_buff=global_tx_data;
	GLOBAL_SPI_WRITE_DATA.tx_bytes=1;
	GLOBAL_SPI_WRITE_DATA.rx_buff=global_rx_data;
	GLOBAL_SPI_WRITE_DATA.rx_bytes=1;
}




uint8_t SPI_WriteByte(uint8_t Byte)
{
    GLOBAL_SPI_WRITE_DATA.tx_bytes=1;
    GLOBAL_SPI_WRITE_DATA.tx_buff[0]=Byte;
    uapi_spi_master_write(0, &GLOBAL_SPI_WRITE_DATA, 0xFFFFFFFF);
    return 0;	
} 



uint8_t SPI_Write320x240x2Bytes(uint8_t* ptrByte)
{

    // osal_printk("start SPI_Write320x240x2Bytes\r\n");
    //short p=0;
    GLOBAL_SPI_WRITE_DATA.tx_bytes=3840;//should be 320*240*2
    
    //osal_printk("SPI_Write320x240Bytes\r\n");
    for(int i=0;i<40;i++){
        GLOBAL_SPI_WRITE_DATA.tx_buff=ptrByte+3840*i;
        uapi_spi_master_write(0, &GLOBAL_SPI_WRITE_DATA, 0xFFFFFFFF);
        // osal_printk("%d, \r\n",i);

        // osal_msleep(2);
    }
    // osal_printk("after SPI_Write320x240x2Bytes\r\n");
    return 0;	
} 
